package com.cuxiaoyi.action;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;

import com.cuxiaoyi.Constants;
import com.cuxiaoyi.db.DBUtils;
import com.cuxiaoyi.db.Dao;
import com.cuxiaoyi.taobao.TaobaoProxy;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.domain.Promotion;
import com.taobao.api.request.MarketingPromotionDeleteRequest;
import com.taobao.api.request.MarketingPromotionsGetRequest;
import com.taobao.api.response.MarketingPromotionDeleteResponse;
import com.taobao.api.response.MarketingPromotionsGetResponse;

public class BatchDeletePromotionsAction extends ActionSupport implements Constants{

	static Logger _log = Logger.getLogger(BatchDeletePromotionsAction.class.getName());
	
	private String numIids;
	
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String sessionId = (String)session.get(TOP_SESSION);
		String[] ids = StringUtils.split(numIids, ',');
		Connection conn = null;
		try
		{
			conn = DBUtils.getConnection();
			for (String numIid : ids)
			{
				try
				{
					Promotion p = Dao.INSTANCE.getPromotion(Long.parseLong(numIid), conn);
					if (p != null)
					{
						deletePromotion(p.getPromotionId(), sessionId, conn);
					}
				}
				catch (Exception e)
				{
					_log.log(Level.SEVERE, "", e);
				}
	//			List<Promotion> promotions = getPromotions(numIid, sessionId);
	//			if (promotions != null)
	//			{
	//				for (Promotion p : promotions)
	//				{
	//					long promotionId = p.getPromotionId();
	//					deletePromotion(promotionId, sessionId);
	//				}
	//			}
			}
		}
		catch (Exception e)
		{
			_log.log(Level.SEVERE, "", e);
		}
		finally
		{
			DBUtils.close(conn);
		}
		return SUCCESS;
	}
	
	private List<Promotion> getPromotions(String numIid, String sessionId) throws ApiException
	{
		MarketingPromotionsGetRequest getReq = new MarketingPromotionsGetRequest();
		getReq.setFields("promotion_id");
		getReq.setNumIid(numIid);
		getReq.setTagId(1L);
		getReq.setStatus("ACTIVE");
		MarketingPromotionsGetResponse getRsp = TaobaoProxy.getClient().execute(getReq, sessionId);
		if (getRsp.isSuccess())
		{
			return getRsp.getPromotions();
		}
		else
		{
			error(getRsp);
		}
		return null;
	}
	
	private void deletePromotion(long promotionId, String sessionId, Connection conn) throws ApiException
	{
		MarketingPromotionDeleteRequest req = new MarketingPromotionDeleteRequest();
		req.setPromotionId(promotionId);
		_log.info("deleting promotion: " + promotionId);
		MarketingPromotionDeleteResponse promotionDeleteResponse = TaobaoProxy.getClient().execute(req, sessionId);
		if (promotionDeleteResponse.isSuccess())
		{
			try
			{
				Dao.INSTANCE.deletePromotion(req.getPromotionId(), conn);
			}
			catch (Exception e)
			{
				_log.log(Level.SEVERE, "", e);
			}
		}
		else
		{
			error(promotionDeleteResponse);
		}
	}
	
	private void error(TaobaoResponse rsp)
	{
		_log.info(rsp.getErrorCode() + " - " + rsp.getMsg() + " - " + rsp.getSubCode() + " - " + rsp.getSubMsg());
	}

	public void setNumIids(String numIids) {
		this.numIids = numIids;
	}

	public String getNumIids() {
		return numIids;
	}
}
